using System;
using System.Configuration;
using System.Windows.Forms;
using System.Xml;
using System.Resources;
using Net.SamuelChen.Libs.DataAccess;
using log4net;

namespace Net.SamuelChen.Production.SmartAssistant {
	/// <summary>
	/// The configuration class.
	/// </summary>
	public class Configuration {
		private static string _locale = "en-us";
		private static string _database;
		private static string _dbScriptPath;
		private static string _dbPassword = "masterkey";
		private static string _dbUser = "SYSDBA";
		private static string _dbConnectionString;
		private static DBMS _dbSystem = DBMS.FireBird;

		private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(Configuration));

		public Configuration() {
		}

		#region properties
		// Locale string such as en-us, zh-cn, etc.
		public static string Locale {
			get { return _locale; }
			set { _locale = value; }
		}

		public static string Database {
			get { return _database; }
			set { _database = value; }
		}

		public static string DBScriptPath {
			get { return _dbScriptPath; }
			set { _dbScriptPath = value; }
		}

		public static string DBUser {
			get { return _dbUser; }
			set { _dbUser = value; }
		}

		public static string DBPassword {
			get { return _dbPassword; }
			set { _dbPassword = value; }
		}

		public static string DBConnectionString {
			get { return _dbConnectionString; }
			set { _dbConnectionString = value; }
		}

		public static DBMS DBSystem {
			get { return _dbSystem; }
			set { _dbSystem = value; }
		}

		#endregion


		#region public methods

		public static void Load() {
			XmlDocument doc = new XmlDocument();
			doc.Load("config.xml");
			
			// locale - Culture
			XmlNode node = doc.GetElementsByTagName("locale").Item(0);
			if (node != null)
				Locale = node.InnerText;

			// database full path
			node = doc.GetElementsByTagName("database").Item(0);
			if (node != null)
				Database = node.InnerText;

			// database system type
			node = doc.GetElementsByTagName("dbsystem").Item(0);
			if (node != null)
				DBSystem = (DBMS)Convert.ToInt32(node.InnerText);

			// database script folder full path
			node = doc.GetElementsByTagName("dbscriptpath").Item(0);
			if (node != null)
				DBScriptPath = node.InnerText;

			// database connection string
			node = doc.GetElementsByTagName("connectionstring").Item(0);
			if (node != null)
				DBConnectionString = node.InnerText;

			// NHibernate O/R Map configuration
			//NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
			//cfg.AddXmlFile(Application.StartupPath + @"\data\mapping\ScheduleItem.hbm.xml");
			//cfg.AddClass(typeof(ScheduleItem));

			log.Info("Configuration loaded.");
		}

		public static void Save() {
			XmlDocument doc = new XmlDocument();
			doc.Load("config.xml");			

			// locale - Culture
			XmlNode node = doc.GetElementsByTagName("dbscriptpath").Item(0);
			if (node != null)
				node.InnerText = Locale;

			// database full path
			node = doc.GetElementsByTagName("database").Item(0);
			if (node != null)
				node.InnerText = Database;

			// database system type
			node = doc.GetElementsByTagName("dbsystem").Item(0);
			if (node != null)
				node.InnerText = DBSystem.ToString();

			// database script folder full path
			node = doc.GetElementsByTagName("dbscriptpath").Item(0);
			if (node != null)
				node.InnerText = DBScriptPath;

			// database connection string
			node = doc.GetElementsByTagName("connectionstring").Item(0);
			if (node != null)
				node.InnerText = DBConnectionString;

			doc.Save("doc.xml");
			log.Info("Configuration saved.");
		}

		// change CurrentUICulture
		public static void ChangeCurrentUICulture(string culture) {
			Configuration.Locale = culture;
			System.Threading.Thread.CurrentThread.CurrentUICulture = 
				new System.Globalization.CultureInfo(culture);
			log.Info("UI Culture changed.");
		}

		#endregion


	}
}
